package com.ydh.host.tsign;

import com.ydh.host.call.ICallback;
import com.ydh.host.processor.IHttpProcessor;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * @author 13001
 */
public class TsignNetworkApi implements IHttpProcessor {

    /**
     * get 请求
     * @param url
     * @param callbask
     */
    @Override
    public void get(String url, ICallback callbask) {
        Observable<ResponseBody> observable = baseApi.get(url);
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBody>() {

                    @Override
                    public void onNext(ResponseBody body) {
                        String content = null;
                        try {
                            content = body.string();
                            callbask.onSuccess(content);
                        } catch (IOException e) {
                            e.printStackTrace();
                            callbask.onFailure(e.getMessage());
                        }
                    }

                    @Override
                    protected void onError(AiThrowable e) {
                        callbask.onFailure(e.getMessage());
                    }
                });
    }

    @Override
    public void get(String url, HashMap<String, String> params, ICallback callbask) {
        Observable<ResponseBody> observable = params == null ? baseApi.get(url) : baseApi.get(url, params);
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBody>() {

                    @Override
                    public void onNext(ResponseBody body) {
                        String content = null;
                        try {
                            content = body.string();
                            callbask.onSuccess(content);
                            if (params != null) {
                                params.clear();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            callbask.onFailure(e.getMessage());
                        }
                    }

                    @Override
                    protected void onError(AiThrowable e) {
                        callbask.onFailure(e.getMessage());
                        if (params != null) {
                            params.clear();
                        }
                    }
                });
    }

    @Override
    public void post(String url, ICallback callbask) {
        Observable<ResponseBody> observable = baseApi.post(url);
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBody>() {

                    @Override
                    public void onNext(ResponseBody body) {
                        String content = null;
                        try {
                            content = body.string();
                            callbask.onSuccess(content);
                        } catch (IOException e) {
                            e.printStackTrace();
                            callbask.onFailure(e.getMessage());
                        }
                    }

                    @Override
                    protected void onError(AiThrowable e) {
                        callbask.onFailure(e.getMessage());
                    }
                });
    }


    /**
     * @param url      接口地址
     * @param params   参数
     * @param callbask 返回
     */
    @Override
    public void post(String url, HashMap<String, String> params, ICallback callbask) {
        Observable<ResponseBody> observable = params == null ? baseApi.post(url) : baseApi.post(url, params);
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBody>() {

                    @Override
                    public void onNext(ResponseBody body) {
                        String content = null;
                        try {
                            content = body.string();
                            callbask.onSuccess(content);
                            if (params != null) {
                                params.clear();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            callbask.onFailure(e.getMessage());
                        }
                    }

                    @Override
                    protected void onError(AiThrowable e) {
                        callbask.onFailure(e.getMessage());
                        if (params != null) {
                            params.clear();
                        }
                    }
                });
    }

    /**
     * put 请求参数
     *
     * @param url
     * @param callback
     */
    @Override
    public void put(String url, ICallback callback) {
        put(url, null, callback);
    }

    @Override
    public void put(String url, HashMap<String, String> params, ICallback callback) {
        Observable<ResponseBody> observable = params == null ? baseApi.get(url) : baseApi.get(url, params);
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBody>() {

                    @Override
                    public void onNext(ResponseBody body) {
                        String content = null;
                        try {
                            content = body.string();
                            callback.onSuccess(content);
                            if (params != null) {
                                params.clear();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            callback.onFailure(e.getMessage());
                        }
                    }

                    @Override
                    protected void onError(AiThrowable e) {
                        callback.onFailure(e.getMessage());
                        if (params != null) {
                            params.clear();
                        }
                    }
                });
    }

    /**
     * del 的请求方式
     *
     * @param url
     * @param callback
     */
    @Override
    public void delete(String url, ICallback callback) {
        delete(url, null, callback);
    }

    @Override
    public void delete(String url, HashMap<String, String> params, ICallback callback) {
        Observable<ResponseBody> observable = params == null ? baseApi.get(url) : baseApi.get(url, params);
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBody>() {

                    @Override
                    public void onNext(ResponseBody body) {
                        String content = null;
                        try {
                            content = body.string();
                            callback.onSuccess(content);
                            if (params != null) {
                                params.clear();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            callback.onFailure(e.getMessage());
                        }
                    }

                    @Override
                    protected void onError(AiThrowable e) {
                        callback.onFailure(e.getMessage());
                        if (params != null) {
                            params.clear();
                        }
                    }
                });
    }

    /**
     * 将 map 转换为 RequestBody
     * @param hashMap
     * @return
     */
    public RequestBody getRequestBody(HashMap<String, String> hashMap) {
        StringBuilder data = new StringBuilder();
        if (hashMap != null && hashMap.size() > 0) {
            Iterator iter = hashMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                Object key = entry.getKey();
                Object val = entry.getValue();
                data.append(key).append("=").append(val).append("&");
            }
        }
        String jso = data.substring(0, data.length() - 1);

        return RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"),jso);
    }

}
